<template>
  <div class="updata-gender">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      :default-index="value"
      @cancel="$emit('close')"
      @confirm="onConfim"
      @change="onPickerChange"
    />
  </div>
</template>

<script>
import { updataUserProfile } from "@/api/user";
export default {
  name: "UpdataGender",
  data() {
    return {
      columns: ["男", "女"],
      localGender: this.value,
    };
  },
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  methods: {
    onPickerChange(picker, value, index) {
      this.localGender = index;
    },
    async onConfim() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true, //静止背景点击
        duration: 0, //持续展示
      });

      try {
        const localGender = this.localGender;
        await updataUserProfile({
          gender: localGender,
        });
        // 更新视图
        this.$emit("input", localGender);
        // 关闭弹层
        this.$emit("close");
        // 提示成功
        this.$toast.success("更新完成");
      } catch (err) {
        this.$toast("更新失败");
      }
    },
  },
};
</script>

<style>
</style>